package com.doge.walknovel.business.login

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.core.view.isVisible
import com.doge.wnpro.R
import com.doge.walknovel.analysis.analysisEvent
import com.doge.walknovel.analysis.login_facebook
import com.doge.walknovel.analysis.login_google
import com.doge.walknovel.core.BaseActivity
import com.doge.walknovel.core.account.FacebookLoginHelper
import com.doge.walknovel.core.account.FacebookUser
import com.doge.walknovel.core.account.GoogleLoginHelper
import com.doge.walknovel.core.account.GoogleUser
import com.doge.walknovel.core.base.isRefreshing
import com.doge.wnpro.databinding.ActivityLoginBinding
import com.doge.walknovel.utils.*

class LoginActivity : BaseActivity() {
    private val binding by lazy { ActivityLoginBinding.inflate(layoutInflater) }
    private val viewModel: LoginViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        InstallReferUtils.check()

        GoogleLoginHelper.registerCallback(googleCallback)
        FacebookLoginHelper.registerCallback(facebookCallback)
        binding.logo.setOnClickCallback {
            EnvHelper.switch()
        }
        binding.loginFacebook.setOnClickCallback {
            FacebookLoginHelper.login(this)
        }
        binding.loginGoogle.setOnClickCallback {
            GoogleLoginHelper.login(this)
        }
        viewModel.loadingGoogle.observe(this) {
            binding.loadingGoogle.isVisible = it.isRefreshing()
        }
        viewModel.loadingFacebook.observe(this) {
            binding.loadingFacebook.isVisible = it.isRefreshing()
        }
        viewModel.success.observe(this) {
            it?.let { finish() }
        }
    }

    private val googleCallback = object : GoogleLoginHelper.Callback {
        override fun onResult(isSuccess: Boolean, data: GoogleUser?, error: String?) {
            log("googleCallback onResult isSuccess $isSuccess data $data error $error")
            toast(error)
            viewModel.loginByGoogle(data, error)
        }
    }

    private val facebookCallback = object : FacebookLoginHelper.Callback {
        override fun onResult(isSuccess: Boolean, data: FacebookUser?, error: String?) {
            log("facebookCallback onResult isSuccess $isSuccess data $data error $error")
            toast(error)
            viewModel.loginByFacebook(data, error)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.logo.setOnClickListener(null)
        binding.loginGoogle.setOnClickListener(null)
        binding.loginFacebook.setOnClickListener(null)
        GoogleLoginHelper.unregisterCallback(googleCallback)
        FacebookLoginHelper.unregisterCallback(facebookCallback)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        GoogleLoginHelper.onActivityResult(requestCode, resultCode, data)
        FacebookLoginHelper.onActivityResult(requestCode, resultCode, data)
    }

    companion object {
        fun start(context: Context?) {
            if (context == null) return
            context.startActivity(Intent(context, LoginActivity::class.java).apply {
                if (context == appContext) flags = Intent.FLAG_ACTIVITY_NEW_TASK
            })
        }
    }
}